libxc: break xc_get_physmem out into os-dependent files
authorChristoph Egger <Christoph.Egger@amd.com>
Thu, 27 Jan 2011 19:03:42 +0000 (19:03 +0000)
committerChristoph Egger <Christoph.Egger@amd.com>
Thu, 27 Jan 2011 19:03:42 +0000 (19:03 +0000)
NetBSD doesn't have sysconf(_SC_PHYS_PAGES).

Factor physmem() out into os-dependent files and rename it to
xc_get_physmem() so as not to pollute the namespace.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
tools/libxc/xc_dom_bzimageloader.c
tools/libxc/xc_linux.c
tools/libxc/xc_netbsd.c
tools/libxc/xc_private.h

index e3ea39f59a2e6e693efe00afdb4aeec05f63a24b..5d949985e032d3ed734e8e49a843faea75d068f8 100644 (file)
@@ -140,27 +140,6 @@ static int xc_try_bzip2_decode(
 
 #include <lzma.h>
 
-static uint64_t physmem(void)
-{
-    uint64_t ret = 0;
-    const long pagesize = sysconf(_SC_PAGESIZE);
-    const long pages = sysconf(_SC_PHYS_PAGES);
-
-    if ( (pagesize != -1) || (pages != -1) )
-    {
-        /*
-         * According to docs, pagesize * pages can overflow.
-         * Simple case is 32-bit box with 4 GiB or more RAM,
-         * which may report exactly 4 GiB of RAM, and "long"
-         * being 32-bit will overflow. Casting to uint64_t
-         * hopefully avoids overflows in the near future.
-         */
-        ret = (uint64_t)(pagesize) * (uint64_t)(pages);
-    }
-
-    return ret;
-}
-
 static int xc_try_lzma_decode(
     struct xc_dom_image *dom, void **blob, size_t *size)
 {
@@ -173,7 +152,7 @@ static int xc_try_lzma_decode(
     int outsize;
     const char *msg;
 
-    ret = lzma_alone_decoder(&stream, physmem() / 3);
+    ret = lzma_alone_decoder(&stream, xc_get_physmem() / 3);
     if ( ret != LZMA_OK )
     {
         DOMPRINTF("LZMA: Failed to init stream decoder");
index 9acf27573e0cfe0f486ada6f84fe761a69173a89..66b055cbe47430f608029070df69269498f648fd 100644 (file)
@@ -55,6 +55,27 @@ void discard_file_cache(xc_interface *xch, int fd, int flush)
     errno = saved_errno;
 }
 
+uint64_t xc_get_physmem(void)
+{
+    uint64_t ret = 0;
+    const long pagesize = sysconf(_SC_PAGESIZE);
+    const long pages = sysconf(_SC_PHYS_PAGES);
+
+    if ( (pagesize != -1) || (pages != -1) )
+    {
+        /*
+         * According to docs, pagesize * pages can overflow.
+         * Simple case is 32-bit box with 4 GiB or more RAM,
+         * which may report exactly 4 GiB of RAM, and "long"
+         * being 32-bit will overflow. Casting to uint64_t
+         * hopefully avoids overflows in the near future.
+         */
+        ret = (uint64_t)(pagesize) * (uint64_t)(pages);
+    }
+
+    return ret;
+}
+
 /*
  * Local variables:
  * mode: C
index 8c82e3664579e08c179321c078561c1bf5def721..76d80595e80a6afc79d0dedb59601688095c452f 100644 (file)
@@ -23,6 +23,9 @@
 #include <xen/sys/evtchn.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/sysctl.h>
 
 static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
 {
@@ -351,6 +354,24 @@ void discard_file_cache(xc_interface *xch, int fd, int flush)
     errno = saved_errno;
 }
 
+uint64_t xc_get_physmem(void)
+{
+    int mib[2], rc;
+    size_t len;
+    uint64_t physmem;
+
+    mib[0] = CTL_HW;
+    mib[1] = HW_PHYSMEM64;
+    rc = sysctl(mib, 2, &physmem, &len, NULL, 0);
+
+    if (rc == -1) {
+        /* PERROR("%s: Failed to get hw.physmem64: %s\n", strerror(errno)); */
+        return 0;
+    }
+
+    return physmem;
+}
+
 static struct xc_osdep_ops *netbsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     switch ( type )
index b01ca2419d0ec27c8c20b9fa46b136fe89018a84..4d19dd6144b87b35a4c8569d91caca3e21bfa75d 100644 (file)
@@ -275,6 +275,9 @@ void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush);
 
+/* How much physical RAM is available? */
+uint64_t xc_get_physmem(void);
+
 #define MAX_MMU_UPDATES 1024
 struct xc_mmu {
     mmu_update_t updates[MAX_MMU_UPDATES];